package com.gwnet.rider.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gwnet.common.core.domain.ResponseResult;
import com.gwnet.common.core.exception.base.BaseException;
import com.gwnet.common.satoken.utils.LoginHelper;
import com.gwnet.rider.domain.RiderRegionRecord;
import com.gwnet.rider.service.RiderRegionRecordService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 骑手负责区域表 前端控制器
 * </p>
 *
 * @author WangLiang
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/riderRegionRecord")
public class RiderRegionRecordController {


    private final RiderRegionRecordService regionRecordService;

    public RiderRegionRecordController(RiderRegionRecordService regionRecordService) {
        this.regionRecordService = regionRecordService;
    }


    /**
     * 新增负责区域
     * @param record
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody RiderRegionRecord record){
        checkRepeat(record);
        // 保存数据
        boolean save = regionRecordService.save(record);
        return ResponseResult.success(save);

    }

    /**
     * 根据ID修改负责的区域
     * @param record
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody RiderRegionRecord record){
        checkRepeat(record);
        boolean flag = regionRecordService.updateById(record);
        return ResponseResult.success(flag);
    }


    /**
     * 根据ID获取记录详情
     * @param record
     * @return
     */
    @PostMapping("/view")
    public ResponseResult<RiderRegionRecord> view(@RequestBody RiderRegionRecord record){
        RiderRegionRecord view = regionRecordService.getById(record.getRecordId());
        return ResponseResult.success(view);
    }


    /**
     * 获取骑手所负责的区域列表
     * 不用分页，负责的区域也是有数量限制的，再说他能负责多少个区域
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<RiderRegionRecord>> list(){
        Long userId = LoginHelper.getLoginUser().getUserId();
        LambdaQueryWrapper<RiderRegionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RiderRegionRecord::getRiderId,userId);
        List<RiderRegionRecord> list = regionRecordService.list(queryWrapper);
        return ResponseResult.success(list);
    }


    /**
     * 根据记录ID删除记录
     * @param record
     * @return
     */
    @PostMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestBody RiderRegionRecord record){
        boolean flag = regionRecordService.removeById(record.getRecordId());
        return ResponseResult.success(flag);
    }

    /**
     * 检查重复
     * @param record 骑手负责范围记录
     */
    private void checkRepeat(RiderRegionRecord record){
        // 获取当前用户
        Long userId = LoginHelper.getLoginUser().getUserId();

        // 查询用户是否添加过该区域的数据
        LambdaQueryWrapper<RiderRegionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RiderRegionRecord::getSchoolId,record.getSchoolId())
                .eq(RiderRegionRecord::getRegionId,record.getRegionId())
                .eq(RiderRegionRecord::getRiderId,userId);
        List<RiderRegionRecord> list = regionRecordService.list(queryWrapper);

        // 添加过此类数据，不允许重复添加
        if(list.size() > 0){
            throw new BaseException("请勿重复添加相同负责区域");
        }
    }








}

